﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Management;

namespace HenIT.BizTalk
{
    public static class BTSWMITools
    {
        #region Private constants
        private const string bts_WMINameSpace = @"root\MicrosoftBizTalkServer";
        private const string bts_HostSettingNameSpace = "MSBTS_HostSetting";
        private const string bts_ServerAppTypeNameSpace = "MSBTS_ServerHost";
        private const string bts_HostInstanceNameSpace = "MSBTS_HostInstance";
        private const string bts_AdapterSettingNameSpace = "MSBTS_AdapterSetting";
        private const string bts_ReceiveHandlerNameSpace = "MSBTS_ReceiveHandler";
        private const string bts_SendHandlerNameSpace = "MSBTS_SendHandler2";
        private const string bts_ServerNameSpace = "MSBTS_Server";
        #endregion

        /// <summary>
        /// Get the list of BizTalk servers in the local group
        /// </summary>
        /// <returns></returns>
        public static List<string> GetGroupServerNames() 
        {
            List<string> servers = new List<string>();

            ManagementClass bts_AdminObjClass = new ManagementClass(bts_WMINameSpace, bts_ServerNameSpace, new ObjectGetOptions());
            foreach (ManagementObject obj in bts_AdminObjClass.GetInstances())
            {
                servers.Add(obj["Name"].ToString());
            }

            return servers;
        }

        public static List<BizTalkHost> GetBizTalkHosts()
        {
            List<BizTalkHost> hosts = new List<BizTalkHost>();

            ManagementClass bts_AdminObjClass = new ManagementClass(bts_WMINameSpace, bts_HostSettingNameSpace, new ObjectGetOptions());
            foreach (ManagementObject obj in bts_AdminObjClass.GetInstances())
            {
                hosts.Add(new BizTalkHost()
                {
                    HostName = obj["Name"].ToString(),
                    NTGroupName = obj["NTGroupName"].ToString(),
                    IsDefault = bool.Parse(obj["IsDefault"].ToString()),
                    HostTracking = bool.Parse(obj["HostTracking"].ToString()),
                    AuthTrusted = bool.Parse(obj["AuthTrusted"].ToString()),
                    HostType = int.Parse(obj["HostType"].ToString()),
                    Is32BitOnly = bool.Parse(obj["IsHost32BitOnly"].ToString())
                }
                );
            }

            return hosts;
        }

        public static List<BizTalkHostInstance> GetBizTalkHostInstances()
        {
            List<BizTalkHostInstance> instances = new List<BizTalkHostInstance>();

            ManagementClass bts_AdminObjClass = new ManagementClass(bts_WMINameSpace, bts_HostInstanceNameSpace, new ObjectGetOptions());
            foreach (ManagementObject obj in bts_AdminObjClass.GetInstances())
            {
                instances.Add(new BizTalkHostInstance()
                {
                    ServerName = obj["RunningServer"].ToString(),
                    HostName = obj["HostName"].ToString(),
                    Logon = obj["Logon"].ToString(),
                    IsDisabled = bool.Parse(obj["IsDisabled"].ToString()),
                    ServiceState = int.Parse(obj["ServiceState"].ToString())
                }
                );
            }

            return instances;
        }

        public static List<BizTalkAdapter> GetBizTalkAdapters()
        {
            List<BizTalkAdapter> adapters = new List<BizTalkAdapter>();
            ManagementClass bts_AdminObjClass = new ManagementClass(bts_WMINameSpace, bts_AdapterSettingNameSpace, new ObjectGetOptions());
            foreach (ManagementObject obj in bts_AdminObjClass.GetInstances())
            {
                adapters.Add(new BizTalkAdapter()
                {
                    Name = obj["Name"].ToString(),
                    Type = obj["MgmtCLSID"].ToString(),
                    Comment = obj["Comment"].ToString()
                }
                );
            }

            return adapters;
        }

        public static List<BizTalkAdapterHandler> GetBizTalkReceiveHandlers()
        {
            List<BizTalkAdapterHandler> receiveHandlers = new List<BizTalkAdapterHandler>();
            ManagementClass bts_AdminObjClass = new ManagementClass(bts_WMINameSpace, bts_ReceiveHandlerNameSpace, new ObjectGetOptions());
            foreach (ManagementObject obj in bts_AdminObjClass.GetInstances())
            {
                receiveHandlers.Add(new BizTalkAdapterHandler()
                {
                    Adapter = obj["AdapterName"].ToString(),
                    HostName = obj["HostName"].ToString()
                }
                );
            }

            return receiveHandlers;
        }

        public static List<BizTalkAdapterHandler> GetBizTalkSendHandlers()
        {
            List<BizTalkAdapterHandler> sendHandlers = new List<BizTalkAdapterHandler>();
            ManagementClass bts_AdminObjClass = new ManagementClass(bts_WMINameSpace, bts_SendHandlerNameSpace, new ObjectGetOptions());
            foreach (ManagementObject obj in bts_AdminObjClass.GetInstances())
            {
                sendHandlers.Add(new BizTalkAdapterHandler()
                {
                    Adapter = obj["AdapterName"].ToString(),
                    HostName = obj["HostName"].ToString()
                }
                );
            }

            return sendHandlers;
        }
    }
}
